﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;

namespace ReceivingDelivering.Receiving
{
    public partial class FrmReceiving : FrmBaseListForm
    {
        public FrmReceiving()
        {
            InitializeComponent();
        }

        public override void InitGridColumns()
        {
            DevExpress.XtraGrid.Columns.GridColumn colReceivingNo = new DevExpress.XtraGrid.Columns.GridColumn();
            colReceivingNo.Caption = "Receiving No";
            colReceivingNo.FieldName = "ReceivingNo";
            colReceivingNo.Name = "colReceivingNo";
            colReceivingNo.Visible = true;
            colReceivingNo.Width = 100;
            colReceivingNo.MinWidth = 100;
            colReceivingNo.OptionsColumn.FixedWidth = true;
            colReceivingNo.VisibleIndex = 0;

            DevExpress.XtraGrid.Columns.GridColumn colCreateDate = new DevExpress.XtraGrid.Columns.GridColumn();
            colCreateDate.Caption = "Create Date";
            colCreateDate.FieldName = "ReceivingCreateDate";
            colCreateDate.Name = "colCreateDate";
            colCreateDate.Visible = true;
            colCreateDate.Width = 100;
            colCreateDate.MinWidth = 100;
            colCreateDate.OptionsColumn.FixedWidth = true;
            colCreateDate.VisibleIndex = 1;

            DevExpress.XtraGrid.Columns.GridColumn colStaffID = new DevExpress.XtraGrid.Columns.GridColumn();
            colStaffID.Caption = "Staff";
            colStaffID.FieldName = "StaffID";
            colStaffID.Name = "colStaffID";
            colStaffID.Visible = true;
            colStaffID.Width = 100;
            colStaffID.MinWidth = 100;
            colStaffID.OptionsColumn.FixedWidth = true;
            colStaffID.VisibleIndex = 2;
            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit rStaff = new DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit();
            rStaff.DataSource = DB.Staffs.ToList();
            rStaff.ValueMember = "StaffID";
            rStaff.DisplayMember = "StaffName";
            colStaffID.ColumnEdit = rStaff;

            DevExpress.XtraGrid.Columns.GridColumn colReason = new DevExpress.XtraGrid.Columns.GridColumn();
            colReason.Caption = "Description";
            colReason.FieldName = "Description";
            colReason.Name = "colReason";
            colReason.Visible = true;
            colReason.Width = 100;
            colReason.MinWidth = 100;
            colReason.OptionsColumn.FixedWidth = true;
            colReason.VisibleIndex = 3;

            DevExpress.XtraGrid.Columns.GridColumn colProjectID = new DevExpress.XtraGrid.Columns.GridColumn();
            colProjectID.Caption = "Project";
            colProjectID.FieldName = "ProjectID";
            colProjectID.Name = "colProjectID";
            colProjectID.Visible = true;
            colProjectID.Width = 100;
            colProjectID.MinWidth = 100;
            colProjectID.OptionsColumn.FixedWidth = true;
            colProjectID.VisibleIndex = 3;
            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit rProject = new DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit();
            rProject.DataSource = DB.Projects.ToList();
            rProject.ValueMember = "ProjectID";
            rProject.DisplayMember = "ProjectNo";
            colProjectID.ColumnEdit = rProject;

            this.gridViewDsDoiTuong.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                colReceivingNo, colCreateDate, colStaffID, colReason, colProjectID});

            foreach (DevExpress.XtraGrid.Columns.GridColumn gc in gridViewDsDoiTuong.Columns)
            {
                gc.FilterMode = DevExpress.XtraGrid.ColumnFilterMode.DisplayText;
                gc.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
            }
        }

        public override void LoadData()
        {
            try
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.ReceivingBills);
                gridDSDoiTuong.DataSource = DB.ReceivingBills.OrderBy(p => p.ReceivingCreateDate).ToList();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        public override void CreateNew()
        {
            try
            {
                FrmEditReceiving frm = new FrmEditReceiving(null);
                frm.RequestChangeInstance += new BasicForms.FrmEditChungTu.OnRequestChangeInstance(LoadData);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Edit()
        {
            try
            {
                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    Lists.ReceivingBill delivering = (Lists.ReceivingBill)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (delivering != null)
                    {
                        FrmEditReceiving frm = new FrmEditReceiving(delivering);
                        frm.RequestChangeInstance += new BasicForms.FrmEditChungTu.OnRequestChangeInstance(LoadData);
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Delete()
        {
            try
            {
                int[] selectedIndexes = gridViewDsDoiTuong.GetSelectedRows();

                if (selectedIndexes != null && selectedIndexes.Length > 0)
                {
                    int selectedRowIndex = selectedIndexes[0];
                    Lists.ReceivingBill deletedObj = (Lists.ReceivingBill)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (deletedObj != null)
                    {
                        if (MessageBox.Show("Are you sure delete " + "'" + deletedObj.ReceivingNo + "'" + "?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No)
                            return;
                        else
                        {
                            DB.ReceivingBills.DeleteOnSubmit(deletedObj);
                            DB.SubmitChanges();
                            LoadData();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show("Cannot delete this voucher!");
            }
        }
    }
}
